/**
 * 删除所有重复的节点，性能最佳版本。
 */
public class JavaDemo4 {
    public static void main(String[] args) {
        int[] arrays = {1, 2, 2, 3, 3, 4, 5, 5};
        ListNode head = new ListNode(arrays[0]);
        ListNode next = head;
        for (int i = 1; i < arrays.length; i++) {
            ListNode node = new ListNode(arrays[i]);
            next.next = node;
            next = node;
        }
        ListNode showParent = head;
        while (showParent != null) {
            System.out.println(showParent.val);
            showParent = showParent.next;
        }
        System.out.println("---------------");
        showParent = deleteDuplicates(head);
        while (showParent != null) {
            System.out.println(showParent.val);
            showParent = showParent.next;
        }
    }

    static public ListNode deleteDuplicates(ListNode head) {
        ListNode res=new ListNode(-1);
        res.next=head;
        ListNode cur=res;
        while (cur.next!=null&&cur.next.next!=null) {
            if (cur.next.val == cur.next.next.val) {
                int temp=cur.next.val;
                while (cur.next!=null&& cur.next.val==temp){
                    cur.next=cur.next.next;
                }
            } else {
                cur = cur.next;
            }
        }
        return res.next;
    }

}
